﻿using ClientTools.Library.DataTypes.Umbraco;

namespace ClientTools.Library
{
    public static class ExtensionMethods
    {
        public static bool IsTrimmedNullOrEmpty(this string value)
        {
            if (value == null) 
                return true;
            return value.Trim() == "";
        }

        public static NodeCollection ToNodeCollection(this umbraco.cms.businesslogic.media.Media[] medias, bool recursive)
        {
            var returnValue = new NodeCollection();
            foreach (var item in medias)
                returnValue.Add(item.ToNode(recursive));
            return returnValue;
        }

        public static Node ToNode(this umbraco.cms.businesslogic.media.Media media, bool recursive)
        {
            var returnValue = new Node
            {
                Id = media.Id,
                Name = media.Text,
                NodeTypeAlias = media.ContentType.Alias,
                NodeTypeId = media.ContentType.Id,
                HasChildren = media.Children.Length > 0
            };
            foreach (var item in media.getProperties)
                returnValue.Properties.Add(item.ToProperty());
            if (recursive)
                returnValue.Children = media.Children.ToNodeCollection(true);
            return returnValue;
        }

        public static NodeCollection ToNodeCollection(this umbraco.cms.businesslogic.web.Document[] docs, bool recursive)
        {
            var returnValue = new NodeCollection();
            foreach (var item in docs)
                returnValue.Add(item.ToNode(recursive));
            return returnValue;
        }

        private static Node ToNode(this umbraco.cms.businesslogic.web.Document doc, bool recursive)
        {
            var returnValue = new Node
            {
                Id = doc.Id,
                Name = doc.Text,
                NodeTypeAlias = doc.ContentType.Alias,
                NodeTypeId = doc.ContentType.Id,
                HasChildren = doc.Children.Length > 0
            };
            foreach (var item in doc.getProperties)
                returnValue.Properties.Add(item.ToProperty());
            if (recursive)
                returnValue.Children = doc.Children.ToNodeCollection(true);
            return returnValue;
        }

        private static Property ToProperty(this umbraco.cms.businesslogic.property.Property prop)
        {
            var returnValue = new Property
                                  {
                                      Id = prop.Id,
                                      PropertyTypeAlias = prop.PropertyType.Alias,
                                      PropertyTypeId = prop.PropertyType.Id
                                  };
            try
            {
                returnValue.Value = (string)prop.Value;
            }
            catch (System.Exception)
            {
                returnValue.Value = "Not Converted";
            }
            return returnValue;
        }
    }
}
